package com.miyake.loveviewweb.client.plugin;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.cell.client.AbstractEditableCell;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.shared.PluginType;

public class PluginListDataGrid extends Composite implements IPluginList {

    private DataGrid<PluginModelEx> dataGrid;
    private List<AbstractEditableCell<?, ?>> editableCells;
    private List<PluginModelEx> data = new ArrayList<PluginModelEx>();

    public PluginListDataGrid() {
        initialize();
    }

    private void initialize() {
        editableCells = new ArrayList<AbstractEditableCell<?, ?>>();

        VerticalPanel panel = new VerticalPanel();
        initWidget(panel);
        panel.setWidth("100%");
        dataGrid = new DataGrid<PluginModelEx>(25);
        panel.add(dataGrid);
        dataGrid.setWidth("100%");
        dataGrid.setHeight("300px");
        //dataGrid.setMinimumTableWidth(140, Unit.EM);
        
        Column<PluginModelEx, Boolean> selColumn = addColumn(new CheckboxCell(), "", new GetValue<Boolean>() {
            public Boolean getValue(PluginModelEx plugin) {
                return plugin.isSelected();
            }
        }, 
        new FieldUpdater<PluginModelEx, Boolean>() {
            public void update(int index, PluginModelEx object, Boolean value) {
                object.setSelected(value);
            }
        });
        dataGrid.setColumnWidth(selColumn, 3.0, Unit.EM);

        
        addColumn(new TextCell(), "Service", new GetValue<String>() {
            public String getValue(PluginModelEx contact) {
                return contact.getPluginModel().getServiceName();
            }
        }, null);
        
        Column<PluginModelEx, String> descColumn = addColumn(new TextCell(), "Description", new GetValue<String>() {
            public String getValue(PluginModelEx contact) {
                return contact.getPluginModel().getDescription();
            }
        }, null);
        dataGrid.setColumnWidth(descColumn, 20.0, Unit.EM);
        
        addColumn(new TextCell(), "Provider", new GetValue<String>() {
            public String getValue(PluginModelEx contact) {
                return contact.getPluginModel().getProvider();
            }
        }, null);
        addColumn(new TextCell(), "Type", new GetValue<String>() {
            public String getValue(PluginModelEx contact) {
                return PluginType.getInstance().getCaption(contact.getPluginModel().getServiceType());
            }
        }, null);
        
        addColumn(new ButtonCell(), "More Info", new GetValue<String>() {

            public String getValue(PluginModelEx contact) {
              return "Detail";
            }
        }, new FieldUpdater<PluginModelEx, String>() {

            public void update(int index, PluginModelEx object, String value) {
                showDetail(object);
            }
        });
          

    }
    
    protected void showDetail(PluginModelEx selected) {
          new BasicDialog(new PluginDetail(selected.getPluginModel())) {
            @Override
            protected void onOK(Composite composite) {

            }
              
          }.showDialog();
    }

    private <C> Column<PluginModelEx, C> addColumn(Cell<C> cell, String headerText,
        final GetValue<C> getter, FieldUpdater<PluginModelEx, C> fieldUpdater) {
      Column<PluginModelEx, C> column = new Column<PluginModelEx, C>(cell) {

        @Override
        public C getValue(PluginModelEx object) {
            return getter.getValue(object);
        }

      };
      column.setFieldUpdater(fieldUpdater);
      if (cell instanceof AbstractEditableCell<?, ?>) {
          editableCells.add((AbstractEditableCell<?, ?>) cell);
      }
      dataGrid.addColumn(column, headerText);
      return column;
    }

    private static interface GetValue<C> {
        C getValue(PluginModelEx contact);
    }

    public Widget getWidgetUi() {
        return this;
    }

    public List<PluginModelEx> getData() {
        return this.data;
    }

    public List<Key> getSelectedKeys() {
        List<Key> keys =new ArrayList<Key>();
        for (PluginModelEx p : data) {
            if (p.isSelected()) {
                keys.add(p.getPluginModel().getKey());
            }
        }
        return keys;
    }

    public void addData(List<PluginModelEx> result) {
        this.data.addAll(result);
        dataGrid.setRowData(this.data); 
        dataGrid.setRowCount(data.size());
    }

    public void clear() {
        this.data.clear();
    }


}
